'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
 
  async getUserInfo() {
    const ctx = this.ctx;

    const [ user ] = await ctx.service.user.getUser({
      _id: ctx.getUserId(),
    });
    if (user && user.user_info) {
      ctx.body = {
        success: true,
        userInfo: user.user_info,
      };
      return;
    }
    ctx.body = {
      success: false,
    }
  }

  async postUserInfo() {
    const ctx = this.ctx;
    const body = ctx.request.body;

    await ctx.service.user.updateUserInfo({
      userId: ctx.getUserId(),
      userInfo: body.userInfo,
    });

    ctx.body = {
      success: true,
    }
  }

  async checksession() {
    const ctx = this.ctx;
    const body = ctx.request.body;
    const token = body.token;
    if (!token) {
      ctx.body = {
        success: false,
      };
      return;
    }
    try {
      const user = await ctx.checkToken(token);
      ctx.body = {
        success: true,
        user,
      };
    } catch (e) {
      ctx.body = {
        success: false,
      };
    }
  }

  async simpleLogin() {
    const ctx = this.ctx;
    const body = ctx.request.body;
    const loginname = body.loginname;

    const [ user ] = await ctx.service.user.getUser({ loginname });
    
    const sessionUser = {
      _id: user._id,
      userId: user._id,
      name: user.name,
    }

    const accessToken = ctx.getToken(sessionUser);

    await ctx.service.user.updateUser({
      userId: user._id,
      accessToken,
    });

    ctx.body = {
      success: true,
      token: accessToken,
    };
  }

  async beikeLogin() {
    const ctx = this.ctx;
    const body = ctx.request.body;
    const token = body.token;
    const ucid = body.ucid;

    const [result, userData] = await ctx.service.user.beikeLogin({ ucid, token });

    const sessionUser = {
      _id: userData.user._id,
      userId: userData.user._id,
      openid: result.openid,
      name: userData.user.name,
    }

    const accessToken = ctx.getToken(sessionUser);

    await ctx.service.user.updateUser({
      accessToken,
      openid: result.openid,
      sessionKey: result.session_key,
    });

    ctx.body = {
      success: true,
      token: accessToken,
    };
  }

  async login() {
    const ctx = this.ctx;

    const body = ctx.request.body;

    const code = body.code;

    const [ result, userData ] = await ctx.service.user.wechatLogin({ code });
    
    const sessionUser = {
      _id: userData.user._id,
      userId: userData.user._id,
      openid: result.openid,
      name: userData.user.name,
    }
    
    const accessToken = ctx.getToken(sessionUser);

    await ctx.service.user.updateUser({
      accessToken,
      openid: result.openid,
      sessionKey: result.session_key,
    });

    ctx.body = {
      success: true,
      token: accessToken,
    };
  }

  async create(ctx) {
    const body = ctx.request.body;

    // 储存新主题帖
    const user = await ctx.service.user.newAndSave(
      body.name,
      body.loginname,
    );

    ctx.body = {
      success: true,
      user_id: user.id,
    };
  }

}

module.exports = UserController;
